package com.example.smartcity.ui.dashboard.Fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.smartcity.R;
import com.example.smartcity.ui.Adapter.AutoLifeGitBannerAdapter;
import com.example.smartcity.ui.Bean.TopicMessageBean;
import com.example.smartcity.ui.dashboard.WaterActivity;
import com.example.smartcity.ui.Tools.OkHttpUtils;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class autoGitFragment extends Fragment {
    private Banner banner;
    private CardView SelfBtn_water;
    private CardView SelfBtn_electricity;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
     View view = inflater.inflate(R.layout.fragment_auto_git, container, false);;
     banner = view.findViewById(R.id.banner);
     SelfBtn_water = view.findViewById(R.id.SelfBtn_water);
     SelfBtn_electricity = view.findViewById(R.id.SelfBtn_electricity);

     initView();
     return view;
    }
    public void initView(){
        //加载Banner
        String newUrl = OkHttpUtils.getBaseUrl(autoGitFragment.this.getContext()) + "/press/press/list?pageNum=1&pageSize=10&pressCategory=48";
        OkHttpUtils.getCall(newUrl).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("TAG", "onFailure: "+e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String Json = response.body().string();
                TopicMessageBean bean = new Gson().fromJson(Json,TopicMessageBean.class);
                List<TopicMessageBean.RowsBean> list = bean.getRows();
                banner.post(new Runnable() {
                    @Override
                    public void run() {
                        banner.setAdapter(new AutoLifeGitBannerAdapter(getContext(),list,R.layout.autohitfragmentbannerlayout))
                        .setIndicator(new CircleIndicator(autoGitFragment.this.getContext()))
                        .setBannerGalleryEffect(15,15,10);
                    }
                });

            }
        });
//        设置水费电费加载的事件
        Intent intent = new Intent(autoGitFragment.this.getContext(), WaterActivity.class);
        SelfBtn_water.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent.putExtra("type","水费");
                startActivity(intent);
            }
        });

        SelfBtn_electricity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent.putExtra("type","电费");
                startActivity(intent);
            }
        });
    }
}